﻿// #define MULTIPLE_VIEWS
// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.ObjectModel;
using BrainTechLLC.ImageOperators;
using BrainTechLLC;
using BrainTechLLC.WPF;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using CPPClassLibrary;
using DirectoryViewModels;
using ImageAppDataModels;
using ImageAppViewModels;
using System.IO;
using System.Collections.Generic;
using System.Text;
using BrainTechLLC.ThreadSafeObjects;
using System.Drawing;
using System.Windows.Media;
using System.Reflection;
using BinaryNorthwest;
using BrainTechLLC.FilterExtensions;
using System.Windows.Controls;
using System.ServiceModel;
using System.Threading;
using System.ServiceModel.Description;

namespace WpfImageDuplicates
{
    public partial class Window1
    {
        public ImageCollectionViewCollection _CollectionViews;
        public ThreadSafeLookup<string, FileImageCollectionViewModel> _FileImageCollectionViewModels;
        public FileImageCollectionViewModelCollection _AllFileImageCollectionViewModels;
        public static bool _doNotStartFileImageCollectionDirectoryReader;

        public FileImageCollectionViewModel FindCollectionViewModel(DirectoryToExamine vm)
        {
            return FileImageCollectionViewModels.FirstOrDefault(o => o.DirectoriesToExamine[0].Directory == vm.Directory);
        }

        public FileImageCollectionViewModel FindImageCollection(DirectoryToExamine vm)
        {
            return FileImageCollectionViewModels.FirstOrDefault(o => o.DirectoriesToExamine[0].Directory == vm.Directory);
        }

        public void ClearFileImageCollections()
        {
            if (_AllFileImageCollectionViewModels != null)
            {
                _AllFileImageCollectionViewModels.ForEach(o => o._stopLazyLoad = true);
            }
            _AllFileImageCollectionViewModels = null;
        }

        public FileImageCollectionViewModelCollection FileImageCollectionViewModels
        {
            get
            {
                if (_AllFileImageCollectionViewModels == null && CurrentDirectoriesToExamine != null)
                {
                    lock (_lock)
                    {
                        if (_AllFileImageCollectionViewModels == null && CurrentDirectoriesToExamine != null)
                        {
                            ThreadSafeLookup<string, FileImageCollectionViewModel> fileImageCollectionViewModels = new ThreadSafeLookup<string, FileImageCollectionViewModel>();
                            ThreadSafeList<string> noLongerExist = new ThreadSafeList<string>();
                            int countGone = 0;
                            var vm = new FileImageCollectionViewModel(CurrentDirectoriesToExamine, this.Dispatcher, null, _doNotStartFileImageCollectionDirectoryReader, (path) =>
                            {
                                if (!_suspendDirsViewModelCollectionChanged)
                                {
                                    _suspendDirsViewModelCollectionChanged = true;
                                    SimpleTimer t = new SimpleTimer(3000, 3000, (tmr) =>
                                    {
                                        if (_suspendDirsViewModelCollectionChanged && noLongerExist.Count == countGone)
                                        {
                                            _suspendDirsViewModelCollectionChanged = false;
                                            Dispatcher.BeginInvoke(() =>
                                            {
                                                if (!_suspendDirsViewModelCollectionChanged)
                                                {
                                                    RefreshDirsViewModel();
                                                }
                                            });
                                            return RunInitializationResult.StopTimer;
                                        }
                                        return RunInitializationResult.DidntDoAnything;
                                    });
                                }

                                directoryCollections.RemoveDirectory(path);
                                countGone++;
                            }, noLongerExist, _CollectionViews[0].Displayed);

                            fileImageCollectionViewModels.Add("*", vm);

                            _FileImageCollectionViewModels = fileImageCollectionViewModels;
                            _AllFileImageCollectionViewModels = new FileImageCollectionViewModelCollection();
                            fileImageCollectionViewModels.AllItems.ForEach(o => _AllFileImageCollectionViewModels.Add(o));

                            if (FilterHandler._currentFilterCollection != null)
                            {
                                SetCurrentFilterCollection(FilterHandler._currentFilterCollection);
                            }
                        }
                    }
                }

                return _AllFileImageCollectionViewModels;
            }
        }

        public ImageCollectionViewCollection CollectionViews
        {
            get
            {
                if (_CollectionViews == null)
                {
                    lock (_lock)
                    {
                        if (_CollectionViews == null)
                        {
                            ImageCollectionViewCollection collectionViews = new ImageCollectionViewCollection();

                            var collectionView = new ImageCollectionView()
                            {
                                Name = "imageCollectionView",
                                HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch,
                                VerticalAlignment = System.Windows.VerticalAlignment.Top,
                                HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left,
                                Margin = new Thickness(2, 2, 2, 2),
                            };

                            collectionView.WantView += new EventHandler<ObjectEventArgs<FileImageViewModel>>(imageCollectionView_WantView);
                            collectionView.RequestDeleteMany += new EventHandler<ObjectEventArgs<List<FileImageViewModel>>>(imageCollectionView_RequestDeleteMany);
                            collectionView.RequestDelete += new EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>>(imageCollectionView_RequestDelete);
                            collectionView.RequestResize += new EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>>(imageCollectionView_RequestResize);
                            collectionView.RequestResizeDelete += new EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>>(imageCollectionView_RequestResizeDelete);
                            collectionView.RequestRotate += new EventHandler<ObjectEventArgsNonRef<RotationParams, bool>>(imageCollectionView_RequestRotate);
                            mainImageCollectionViewGrid.Children.Insert(0, collectionView);
                            collectionViews.Add(collectionView);

                            _CollectionViews = collectionViews;
                        }
                    }
                }
                return _CollectionViews;
            }
            set
            {
                if (_CollectionViews != value)
                {
                    if (_CollectionViews != null)
                    {
                        for (int n = 0; n < _CollectionViews.Count; n++)
                        {
                            var collectionView = _CollectionViews[n];
                            if (collectionView._addTimer != null)
                            {
                                collectionView._addTimer.DisposeTimer();
                            }
                            if (collectionView._moveTimer != null)
                            {
                                collectionView._moveTimer.DisposeTimer();
                            }
                            if (collectionView.ThumbsHandler != null)
                            {
                                if (collectionView.ThumbsHandler.ThumbPreloader != null)
                                {
                                    collectionView.ThumbsHandler.ThumbPreloader.Exit();
                                }
                                if (collectionView.ThumbsHandler.ThumbCleanup != null)
                                {
                                    collectionView.ThumbsHandler.ThumbCleanup.Stop();
                                }
                            }
                            collectionView.WantView -= imageCollectionView_WantView;
                            collectionView.RequestDeleteMany -= imageCollectionView_RequestDeleteMany;
                            collectionView.RequestDelete -= imageCollectionView_RequestDelete;
                            collectionView.RequestResize -= imageCollectionView_RequestResize;
                            collectionView.RequestResizeDelete -= imageCollectionView_RequestResizeDelete;
                            collectionView.RequestRotate -= imageCollectionView_RequestRotate;
                            collectionView.DecoupleEvents();
                            mainImageCollectionViewGrid.Children.Remove(collectionView);
                        }
                    }

                    _CollectionViews = value;
                }
            }
        }
    }

    public class FileImageCollectionViewModelCollection : ObservableCollection<FileImageCollectionViewModel>
    {
        public bool ViewModelCollectionChanged;

        public FileImageViewModel Find(string fileName)
        {
            FileImageViewModel found = null;

            for (int n = 0; n < this.Count; n++)
            {
                found = this[n].Find(fileName);
                if (found != null)
                {
                    return found;
                }
            }
            return found;
        }
    }

    public class ImageCollectionViewCollection : ObservableCollection<ImageCollectionView>
    {
        public bool ItemSelectionChanged;
        public bool ViewModelCollectionChanged;

        public void OnItemSelectionChanged()
        {
            ItemSelectionChanged = true;
        }

        public void OnViewModelCollectionChanged()
        {
            ViewModelCollectionChanged = true;
        }

        public ThumbnailView FindThumbView(string fileName)
        {
            for (int n = 0; n < this.Count; n++)
            {
                var o = this[n];
                var found = o.FindThumbView(fileName);
                if (found != null)
                {
                    return found;
                }
            }
            return null;
        }
    }
}


//public FileImageCollectionViewModelCollection FileImageCollectionViewModels
//{
//    get
//    {
//        if (_AllFileImageCollectionViewModels == null && DirectoriesToExamine != null)
//        {
//            lock (_lock)
//            {
//                if (_AllFileImageCollectionViewModels == null && DirectoriesToExamine != null)
//                {
//                    ThreadSafeLookup<string, FileImageCollectionViewModel> fileImageCollectionViewModels = new ThreadSafeLookup<string, FileImageCollectionViewModel>();
//                    for (int n = 0; n < DirectoriesToExamine.Count; n++)
//                    {
//                        var dir = DirectoriesToExamine[n];
//                        DirectoriesToExamine dirs = new ImageAppDataModels.DirectoriesToExamine(new string[] { dir.Directory });
//                        var vm = new FileImageCollectionViewModel(new DirectoriesToExamineViewModel(dirs), this.Dispatcher, null, _doNotStartFileImageCollectionDirectoryReader);
//                        fileImageCollectionViewModels.Add(dir.Directory, vm);
//                    }

//                    if (FilterHandler._currentFilterCollection != null)
//                    {
//                        SetCurrentFilterCollection(FilterHandler._currentFilterCollection);
//                    }

//                    _FileImageCollectionViewModels = fileImageCollectionViewModels;
//                    _AllFileImageCollectionViewModels = new FileImageCollectionViewModelCollection();
//                    fileImageCollectionViewModels.AllItems.ForEach(o => _AllFileImageCollectionViewModels.Add(o));
//                }
//            }
//        }

//        return _AllFileImageCollectionViewModels;
//    }
//}

//public ImageCollectionViewCollection CollectionViews
//{
//    get
//    {
//        if (_CollectionViews == null)
//        {
//            lock (_lock)
//            {
//                if (_CollectionViews == null)
//                {
//                    ImageCollectionViewCollection collectionViews = new ImageCollectionViewCollection();

//                    for (int n = 0; n < DirectoriesToExamine.Count; n++)
//                    {
//                        DirectoryToExamineViewModel dir = DirectoriesToExamine[n];
//                        var collectionView = new ImageCollectionView()
//                        {
//                            DirectoryToExamine = dir._dir,
//                            Name = "imageCollectionView" + n.ToString(),
//                            HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch,
//                            Height = double.NaN,
//                            VerticalAlignment = System.Windows.VerticalAlignment.Stretch,
//                            HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left,
//                            Margin = new Thickness(0, 0, 0, 4)
//                        };

//                        collectionView.WantView += new EventHandler<ObjectEventArgs<FileImageViewModel>>(imageCollectionView_WantView);
//                        collectionView.RequestDeleteMany += new EventHandler<ObjectEventArgs<List<FileImageViewModel>>>(imageCollectionView_RequestDeleteMany);
//                        collectionView.RequestDelete += new EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>>(imageCollectionView_RequestDelete);
//                        collectionView.RequestResize += new EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>>(imageCollectionView_RequestResize);
//                        collectionView.RequestResizeDelete += new EventHandler<ObjectEventArgsNonRef<FileImageViewModel, bool>>(imageCollectionView_RequestResizeDelete);
//                        collectionView.RequestRotate += new EventHandler<ObjectEventArgsNonRef<RotationParams, bool>>(imageCollectionView_RequestRotate);
//                        mainImageCollectionViewGrid.Children.Insert(0, collectionView);
//                        if (mainImageCollectionViewGrid.RowDefinitions.Count < n)
//                        {
//                            mainImageCollectionViewGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1D, GridUnitType.Auto) });
//                        }
//                        Grid.SetRow(collectionView, n);
//                        collectionViews.Add(collectionView);
//                    }
//                    _CollectionViews = collectionViews;
//                }
//            }
//        }
//        return _CollectionViews;
//    }
//    set
//    {
//        if (_CollectionViews != value)
//        {
//            if (_CollectionViews != null)
//            {
//                for (int n = 0; n < _CollectionViews.Count; n++)
//                {
//                    var collectionView = _CollectionViews[n];
//                    collectionView.WantView -= imageCollectionView_WantView;
//                    collectionView.RequestDeleteMany -= imageCollectionView_RequestDeleteMany;
//                    collectionView.RequestDelete -= imageCollectionView_RequestDelete;
//                    collectionView.RequestResize -= imageCollectionView_RequestResize;
//                    collectionView.RequestResizeDelete -= imageCollectionView_RequestResizeDelete;
//                    collectionView.RequestRotate -= imageCollectionView_RequestRotate;
//                    collectionView.DecoupleEvents();
//                    mainImageCollectionViewGrid.Children.Remove(collectionView);
//                }
//            }

//            _CollectionViews = value;
//        }
//    }
//}
